package mysql

import (
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"user_srv/appconfig"
)

var DB *gorm.DB

func Mysql() {
	var err error
	var MysqlConf = appconfig.AppCon.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		MysqlConf.User, MysqlConf.Password, MysqlConf.Host, MysqlConf.Port, MysqlConf.Database)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	err = DB.AutoMigrate(&User{}, &UserRealname{})
	if err != nil {
		logx.Errorf("迁移建表失败")
		return
	}
	logx.Errorf("迁移建表成功")
	logx.Errorf("数据库链接成功")
}
